--#include "data\config\equip\EquipInjectConfig.lua" once
--#include "data\config\attr\FuncAttrsConfig.lua" once

--[[装备注灵
]]
function EquipInject(sysarg, itemPtr )
	--print("EquipInject ...")
	if not itemPtr then
		return
	end

	local itemType 			= Item.getItemProperty( sysarg, itemPtr, Item.ipItemType, 0)
	local itemInject  		= Item.getItemProperty( sysarg, itemPtr, Item.ipItemInjectLevel, 0)			--注灵等级
	local itemInjectLimit	= Item.getItemProperty( sysarg, itemPtr, Item.ipItemInjectLimit, 0)			--该装备可注灵限制(取配置，不大于itemStarMax)
	local itemInjectMax		= GetEquipInjectMaxByType( itemType )										--该类型装备可强化的最大星级
	local doInjectMax		= 0																		--扣除损失星级是上限
	if itemInjectLimit == 0 or itemInjectLimit > itemInjectMax then
		doInjectMax = itemInjectMax
	else
		doInjectMax = itemInjectLimit
	end

	--print("EquipInject, itemType="..itemType..", itemInject="..itemInject..", itemInjectLimit="..itemInjectLimit..", itemInjectMax="..itemInjectMax..
	--	", doInjectMax="..doInjectMax)
	if itemType < Item.itWeapon or itemType >= Item.itEquipMax then				--不是装备不能强化
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipInject001, ttFlyTip)
		return
	end

	if itemInject >= doInjectMax then												--装备最大属性
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipInject002, ttFlyTip)
		return
	end

	local nextInject = itemInject + 1
	local injectConsumes = EquipInjectCfg.injectConsume[nextInject]
	if not injectConsumes then
		return
	end

	if not CheckConsumeCond( sysarg, injectConsumes ) then
		return 
	end

	if not DoConsumeCond( sysarg, injectConsumes, 1, 1154, Lang.ScriptTips.EquipLog005  ) then
		return 
	end

	Item.setItemProperty( sysarg, itemPtr, Item.ipItemInjectLevel, nextInject)
	Actor.triggerQuestEvent(sysarg, 48, 1, 1) 						--任务事件-注灵1次
	Actor.triggerAchieveEvent(sysarg, 46, 3, nextInject)			--成就目标达成事件，注灵等级

	local tips = string.format(Lang.ScriptTips.EquipInject004, nextInject)
	Actor.sendTipmsg(sysarg, tips, ttFlyTip)
end

--[[装备注灵回收
]]
function EquipInjectRecycle(sysarg, itemPtr )
	if not itemPtr then
		return
	end

	if Item.isFurnaceEquip( itemPtr ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipInject007, ttFlyTip)
		return
	end

	local itemInject  = Item.getItemProperty( sysarg, itemPtr, Item.ipItemInjectLevel, 0)			--注灵等级
	--print("EquipInjectRecycle, itemInject="..itemInject)

	if itemInject <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipInject005, ttFlyTip)
		return
	end

	local recycleBackS = EquipInjectCfg.recycleBack[itemInject]
	if not recycleBackS then
		return
	end

	if not IsBagGridEnough(sysarg, recycleBackS) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
	end

	Item.setItemProperty( sysarg, itemPtr, Item.ipItemInjectLevel, 0)				--注灵等级清0
	local itemInjectNew  = Item.getItemProperty( sysarg, itemPtr, Item.ipItemInjectLevel, 0)			--注灵回收后的注灵等级
	if itemInjectNew ~= 0 then			--再次检查一下注灵
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipInject008, ttFlyTip)
		return
	end

	GiveCommonAward(sysarg, recycleBackS, 1155, Lang.ScriptTips.EquipLog006)
	Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipInject006, ttFlyTip)

	SendEquipInjectResult(sysarg, 2, 1)				--成功才发
end

function GetEquipInjectMaxByType( itemType )
	return #FuncAttrsCfg[enFunctionAttrType_EquipInject][itemType]
end


--装备升星结果
function  SendEquipInjectResult(sysarg, InjectType, result)
	local netPack = DataPack.allocPacket(sysarg, enEquipSystemID, sEquipInjectResult)
	if netPack then
		DataPack.writeByte(netPack,InjectType)
		DataPack.writeByte(netPack,result)
		DataPack.flush(netPack)
	end
end
